import { CustomerInfo } from "@/pages/index/type";
import { useRequest } from "ahooks"
import { fetchMyStarList } from './service'
import { LoadMoreFormatReturn } from '@ahooksjs/use-request/lib/types';

function useData() {
  const { data: customerList } = useRequest((d) => queryCustomerList(d), {
    loadMore: true,
    isNoMore: (r) => !r?.hasMore
  })

  return {
    customerList: customerList?.list || []
  }
}

export default useData

async function queryCustomerList(d?: LoadMoreFormatReturn) {
  const { current = 0 } = d || { current: 0 };
  const { res } = await fetchMyStarList({
    size: 10,
    current,
  });

  if (res) {
    return {
      list: res.records,
      current: res.current + 1,
      hasMore: res.total > res.pages * res.size,
    };
  }
  return {
    list: [] as CustomerInfo[],
    hasMore: false,
  };
}